package app.game;

import java.awt.Graphics;
import java.awt.Rectangle;

public class Entity {

    /**
     * The current x location of this entity
     */
    protected double x;
    /**
     * The current y location of this entity
     */
    protected double y;
    /**
     * The sprite that represents this entity
     */
    protected Sprite sprite;
    /**
     * The current speed of this entity horizontally (pixels/sec)
     */
    protected double dx;
    /**
     * The current speed of this entity vertically (pixels/sec)
     */
    protected double dy;
    /**
     * The rectangle used for this entity during collisions resolution
     */
    private Rectangle me = new Rectangle();
    /**
     * The rectangle used for other entities during collision resolution
     */
    private Rectangle him = new Rectangle();

    /**
     * Construct a entity based on a sprite image and a location.
     *
     * @param ref The reference to the image to be displayed for this entity
     * @param x The initial x location of this entity
     * @param y The initial y location of this entity
     */
    public Entity(String ref, int x, int y) {
        this.sprite = SpriteStore.get().getSprite(ref);
        this.x = x;
        this.y = y;
    }

    /**
     * Request that this entity move itself based on a certain ammount of time
     * passing.
     *
     * @param delta The ammount of time that has passed in milliseconds
     */
    public void move(long delta) {
        // update the location of the entity based on move speeds
        x += (delta * dx) / 1000;
        y += (delta * dy) / 1000;
    }

    public void setHorizontalMovement(double dx) {
        this.dx = dx;
    }

    public void setVerticalMovement(double dy) {
        this.dy = dy;
    }

    public double getHorizontalMovement() {
        return dx;
    }

    public double getVerticalMovement() {
        return dy;
    }

    public void draw(Graphics g) {
        sprite.draw(g, (int) x, (int) y);
    }

    public void doLogic() {
    }
    /* 
     * Getters Setters for the parameters
     */
    public int getX() {
        return (int) x;
    }

    public int getY() {
        return (int) y;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }
    
    public int getWidth() {
        return sprite.getWidth();
    }

    public int getHeight() {
        return sprite.getHeight();
    }
    
    /*
     * Method that returns true if the one sprite collides with the other. That help us
     * to calculate the health bar for each instance of game players.
     * 
    */
    public boolean collidesWith(Entity other) {
        me.setBounds((int) x, (int) y, sprite.getWidth(), sprite.getHeight());
        him.setBounds((int) other.x, (int) other.y, other.sprite.getWidth(), other.sprite.getHeight());

        return me.intersects(him);
    }
}